package com.bzframework.project.api.web;

import com.bzframework.pro.api.core.provider.ApiSupport;
import com.bzframework.pro.api.core.provider.ApiSupportManager;
import com.bzframework.pro.authorize.adapter.ReactiveServiceCrudControllerAdapter;
import com.bzframework.pro.authorize.annotation.Authorization;
import com.bzframework.project.api.entity.OpenApiEntity;
import com.bzframework.project.api.service.OpenApiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.hswebframework.web.crud.service.ReactiveCrudService;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author bingzhong
 * @Date 2025/3/3 14:20
 * @Description
 */
@Tag(name = "openapi", description = "openapi")
@RestController
@RequestMapping("/openapi")
@RequiredArgsConstructor
public class OpenApiController implements ReactiveServiceCrudControllerAdapter<OpenApiEntity, String> {

    private final ApiSupportManager apiSupportManager;

    private final OpenApiService openApiService;
    @Override
    public ReactiveCrudService<OpenApiEntity, String> getService() {
        return openApiService;
    }

    @GetMapping("/supports")
    @Authorization(ignore = true)
    public Flux<ApiSupport> getApiSupports() {
        return Flux.fromIterable(apiSupportManager.getApiSupports());
    }

    @PostMapping("/upload")
    @Authorization(ignore = true)
    @Operation(summary = "上传 ApiSupport jar包")
    public Mono<String> uploadApiSupportJar(@RequestPart("file")FilePart filePart) {
        return openApiService.uploadApiSupportJar(filePart);
    }

    @GetMapping("/load")
    @Authorization(ignore = true)
    @Operation(summary = "加载 ApiSupport jar包")
    public Mono<Boolean> loadApiSupportJar(@RequestParam String jarPath) {
        return openApiService.loadApiSupportJar(jarPath);
    }

}
